package org.apple.console;

import java.util.ArrayList;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

public class GraphPanel extends JPanel {

	class Location {
		int x;
		int y;

		public Location(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}
	
	protected ArrayList<String> datas;
	protected int maxPointNumber;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6511963911520784397L;
	private String name;
	private String trueName;
	
	public GraphPanel(String name) {
		super();
		this.name = name;
		this.trueName = name;
		this.datas = new ArrayList<String> ();
		this.maxPointNumber = 50; 
	}
	
	public void appendPositionInfo(String positionInfo){
		this.name = this.trueName + "___Position: " + positionInfo;
	}
	
	public void paintBase(Graphics g) {
		g.setColor(Color.green);
		
		Dimension panelSize = getSize();
		g.drawString(name, panelSize.width * 3 / 10, panelSize.height * 1 / 10);
		Location zeroLocation = new Location(panelSize.width * 1 / 10, 
											panelSize.height * 9 / 10);
		Location xDestLocation = new Location(panelSize.width * 9 / 10, 
											panelSize.height * 9 / 10);
		Location yDestLocation = new Location(panelSize.width * 1 / 10, 
											panelSize.height * 1 / 10);
		//Double[] data = {0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0};
		
		String data = "0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0";
	    String datas = "0     5    10    15     20      25      30     35     40     45    50";
		g.drawChars(datas.toCharArray(), 0, datas.length(), zeroLocation.x, zeroLocation.y+10);
		//g.drawBytes(data, offset, length, x, y)
		// draw x line
		g.drawLine(zeroLocation.x, zeroLocation.y, xDestLocation.x, xDestLocation.y);
		String [] temps = data.split(" ");
		for(int i = 0; i < temps.length; i++){
			// char[] temp = {data[i].byteValue()};
			// System.out.println(temps[i]);
			g.drawChars(temps[i].toCharArray(), 0, temps[i].length(), zeroLocation.x - 10, zeroLocation.y - i*20);
			
		}
		// draw y line
		g.drawLine(zeroLocation.x, zeroLocation.y, yDestLocation.x, yDestLocation.y);
	}
	
	protected void paintDatas(Graphics g) {
		int number = datas.size();
		int[] x = new int[number];
		int[] y = new int[number];
		translateTo(x, y);

		g.setColor(Color.green);
		g.drawPolyline(x, y, datas.size());
	}
	
	protected void translateTo(int[] x, int[] y) {
		Dimension panelSize = getSize();
		int xStep = panelSize.width / maxPointNumber;
		Location zeroLocation = new Location(panelSize.width * 1 / 10, 
				panelSize.height * 9 / 10);

		double[] tempData = new double[datas.size()];
		double min = Double.MIN_VALUE;
		double max = Double.MAX_VALUE;
		for (int i = 0; i < tempData.length; i ++) {
			tempData[i] = Double.parseDouble(datas.get(i));
			min = min < tempData[i] ? min : tempData[i];
			max = max > tempData[i] ? max : tempData[i];
		}
		// int margin = max - min;
		// int drawMargin = panelSize.height * 8 / 10;

		for (int i = 0; i < tempData.length; i ++) {
			if (i == 0)
				x[i] = zeroLocation.x;
			else
				x[i] = x[i - 1] + xStep;
			
			double base = panelSize.width * 1.0 / 10;
			double added = (1 - tempData[i]) * (panelSize.height * 8 / 10); //(max - tempData[i]) * 8 / (10 * (max - min));
			
			y[i] = (int)(base + added);
		}
		
	}
	
	public void receiveData(String data) {
		if (this.datas.size() > this.maxPointNumber)
			this.datas.remove(0);
		this.datas.add(data);
		repaint();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		// Graphics2D g2 = (Graphics2D)g;
		setBackground(Color.black);
		paintBase(g);
		paintDatas(g);
	}
}
